import router from '../../router/index'
const regPhone = /(^((\+86)|(86))?(1[3-9])\d{9}$)|(^(0\d{2,3})-?(\d{7,8})$)/

// 校验手机号码的正则
export const reg_tel_phone = regPhone

// form表单校验手机号的规则
export const phoneFormRule = {
  required: true,
  pattern: regPhone,
  message: '请输入正确的手机号码',
  trigger: 'change'
}

// 拷贝对象
export const copyObj = function (obj) {
  return JSON.parse(JSON.stringify(obj))
}

/**
 * 设置用户信息到缓存
 * @param {param} 登录或者切换登录返回的res.data
 */
export const setUserInfo = function (param) {
  // localStorage.setItem('token', param.sso_token)
  // let user = param.user
  // if (param.employee && param.employee != 'null') {
  //   user.roleId = param.employee.stationId;
  //   user.merchantId = param.employee.merchantId;
  // } else {
  //   user.roleId = user.stationId ? user.stationId : param.employeeList[0] ? param.employeeList[0].stationId : '';
  //   user.stationId = user.stationId ? user.stationId : param.employeeList[0] ? param.employeeList[0].stationId : '';
  // }
  // user.shop=param.shop;
  // user.roleList = param.employeeList;
  localStorage.setItem('info', JSON.stringify(param))

}

/**
 * 从缓存获取用户信息
 * @param {param} 想获得用户什么信息，就穿入对应的字段，比如要获取merchantId => getUserInfo('merchantId') ,不传就返回整个用户信息对象。
 * 此方法无需引入，组件内直接使用 => getUserInfo() / getUserInfo('merchantId')
 */
export const getUserInfo = function (param) {
  let userInfo = JSON.parse(localStorage.getItem('info'))
  if(userInfo==null){
    router.push('/login')
  }
  if (param&&userInfo) return userInfo[param]
  return userInfo

}

/**
 * 获取按钮权限 
 * @param {name} 按钮名称。
 */
export const havePermission = function (name) {
  if (!name) return false
  let url = window.location.href.split('#')[1]
  let index
  let menu = useMainStore().menu;
  menu.forEach(i => {
    if (i.menuType == 0 && i.children) {
      let childrenItem = i.children.find(j => {
        let path = i.url + j.url
        if (path == url) return j
      })
      if (childrenItem) {
        // permission
        index = childrenItem.permission.findIndex(k => k.name == name)
      }
    }
  })
  return index > -1
}



export const loginOut = function (name) {
  $axios.post('/sel/logout', {
  }).then(res => {
    localStorage.removeItem("info")
    router.push('/login')
  })
  //location.reload()
}

/**
 * 金额转换，保留两位小数
 * @param {amount} 任意字符串或数组。
 */
export const formatAmount = (amount) => {
  if (amount == '') return 0
  //修复第一个字符是小数点 的情况.
  if (amount != '' && amount.substr(0, 1) == '.') {
    amount = "";
  }
  amount = amount.replace(/^0*(0\.|[1-9])/, '$1');  //解决 粘贴不生效
  amount = amount.replace(/[^\d.]/g, "");   //清除“数字”和“.”以外的字符
  amount = amount.replace(/\.{2,}/g, ".");  //只保留第一个. 清除多余的     
  amount = amount.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
  amount = amount.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');  //只能输入两个小数

  if (amount.indexOf(".") < 0 && amount != "") {  //以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
    if (amount.substr(0, 1) == '0' && amount.length == 2) {
      amount = amount.substr(1, amount.length);
    }
  }

  
  if (amount) {
    let maxPrice = 99999999.99
    return +amount > maxPrice ? maxPrice : amount
  } else {
    return 0
  }
}